package org.samcolon.eeexplore.hutool;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.lang.Console;

import java.util.Scanner;

/**
 * @Description: 图型验证码
 * @author: Sam.zhang
 * @date: 2021.09.03
 */
public class CaptchaUtilTest {
    public static void main(String[] args) {
        RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        lineCaptcha.setGenerator(randomGenerator);

        //图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write("d:/sam-temp/verification.png");
        //输出code
        Console.log(lineCaptcha.getCode());
        //验证图形验证码的有效性，返回boolean值
        Console.log(lineCaptcha.verify("1234"));
        Console.log(lineCaptcha.getImageBase64());


        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
        captcha.setGenerator(new MathGenerator(1));
        captcha.createCode();

        //图形验证码写出，可以写出到文件，也可以写出到流
        captcha.write("d:/sam-temp/verification2.png");
        //输出code
        Console.log(captcha.getCode());
        //验证图形验证码的有效性，返回boolean值
        Scanner input = new Scanner(System.in);
        Console.log(captcha.verify(input.next()));
        Console.log(captcha.getImageBase64());
    }
}
